package cn.eleven.service.impl;
import cn.eleven.service.NewsService;
import cn.eleven.entity.News;
import cn.eleven.mapper.NewsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService{
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public IPage<News> search(News news) {
        String title = news.getTitle();
        String category = news.getCategory();
        //String content = news.getContent();
        Integer pageNum = news.getPageNum();
        Integer pageSize = news.getPageSize();


        QueryWrapper<News> wrapper = new QueryWrapper<>();
        //判断参数是否为空,如果不为空才根据条件进行查询
        wrapper.like(title!=null,"title", title)
//                .like(updatedAt!=null,"updatedAt",updatedAt)
                .like(category!=null,"category", category);

        IPage<News> page = new Page<>(pageNum,pageSize);
        //要进行分页查询，使用page对象
        IPage<News> newsIPage = newsMapper.selectPage(page, wrapper);
        return newsIPage;
    }
}
